Ext.define('cloud.controller.Category', {
    extend: 'Ext.app.Controller',
    config: {
        file: '',
        refs: {
            category: 'category',
            categoryedit: 'categoryedit',
            categoryadd: 'categoryadd',
            category_add: 'category #category_add',
            category_upload_icon: 'category #category_upload_icon',
            category_loadedImage: 'category #loadedImage',
            category_submit: 'categoryadd #category_submit',
            category_edit_submit: 'categoryedit #category_edit_submit',
            category_del: 'categoryedit #category_del',
            select: 'category #category_select'
        },
        control: {
            /**
             * show categoryadd page
             */
            category_add: {
                tap: function () {
                    var categoryadd = this.getCategoryadd()||Ext.create('cloud.view.category.CategoryAdd');
                    this.getCategory().push(categoryadd);
                }
            },
            /**
             * show categoryedit page
             */
            select: {
                itemtap: function (list, item, index, record) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var categoryedit = this.getCategoryedit()||Ext.create('cloud.view.category.CategoryEdit');
                    Ext.getCmp('catagory_loadedImage').setSrc(record.getData().iconUrl);
                    categoryedit.setRecord(record);
                    this.getCategory().push(categoryedit);
                    Ext.Viewport.unmask();
                }
            },
            /**
             * add category
             */
            category_submit: {
                tap: function () {

                    var name = Ext.getCmp('category_name').getValue();
                    if (Ext.isEmpty(name)) {
                        Ext.Msg.alert('请输入分类名称');
                        return
                    }
                    if (Ext.isEmpty(Ext.getCmp('category_display').getValue())) {
                        Ext.Msg.alert('请输入排序');
                        return
                    }
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getCategoryadd().submit({
                        url: GLOBAL.api_url + 'category_add?cadid=' + localStorage.getItem('cadid'),
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });


                }
            },
            /**
             * edit category
             */
            category_edit_submit: {
                tap: function () {

                    var name = Ext.getCmp('category_name').getValue();
                    if (Ext.isEmpty(name)) {
                        Ext.Msg.alert('请输入分类名称');
                        return
                    }
                    if (Ext.isEmpty(Ext.getCmp('categoryedit_display').getValue())) {
                        Ext.Msg.alert('请输入排序');
                        return
                    }
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getCategoryedit().submit({
                        url: GLOBAL.api_url + 'category_update',
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });
                }
            },
            /**
             * delete category
             */
            category_del: {
                tap: function () {
                    Ext.Msg.confirm('是否删除', '', function (data) {
                        if (data == 'yes') {
                            this.getCategoryedit().submit({
                                url: GLOBAL.api_url + 'category_delete',
                                scope: this,
                                method: 'POST',
                                success: function (from, result) {
                                    if (result.Variables.Result.code == 0) {
                                        Ext.getStore('CategoryAllFindStore').load({params: {cadid: localStorage.getItem('cadid')}});
                                        this.getCategory().pop();
                                    } else {
                                        Ext.Msg.alert(result.Variables.Message.messagestr);
                                    }
                                }
                            });
                        }
                    }, this);
                }
            },
            category_upload_icon: {
                loadsuccess: function (dataurl, m, e, file) {
                    var me = this;
                    this.setFile(file);
                    var image = me.getCategory_loadedImage();
                    image.setSrc(dataurl);
                }
            }
        },
        pagePicker: undefined,
        id: undefined
    },

    executeSuccess: function (from, result) {
        if (result.Variables.Result.code == 0) {
            // 上传图片
            var ccid = result.Variables.data.ccid;
            if (Ext.isEmpty(ccid)) {
                ccid = Ext.getCmp('hidden_id').getValue();
            }
            this.doUpload(this.getFile(), ccid);
        } else {
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
    },
    doUpload: function (file, ccid) {
        var me = this;
        var http = new XMLHttpRequest();
        var form = new FormData();
        form.append('Filedata', file);
        var url = GLOBAL.api_url + 'product_category_upload&ccid=' + ccid;
        if (http.upload && http.upload.addEventListener) {
            // Response handler
            http.onreadystatechange = function (e) {

                if (this.readyState == 4) {
                    if (this.status == 200) {
                        var returnText = Ext.decode(this.responseText);
                        if (returnText.Variables.Result.code == 0) {
                            var attachid = returnText.Variables.data.attachid;
                            Ext.getStore('CategoryAllFindStore').load({params: {cadid: localStorage.getItem('cadid')}});  // 产品分类
                            console.log('上传图片ID为 ' + attachid + ' 成功');
                            Ext.getStore('CategoryAllFindStore').load({params: {cadid: localStorage.getItem('cadid')}});
                            me.getCategory().pop();
                            Ext.Viewport.unmask();
                        }
                    }
                }
            };
            // Error handler
            http.upload.onerror = this.actionfailure;
        }
        http.open('POST', url);
        http.send(form);
        this.setFile('');
    },
    actionfailure: function () {//请求失败时执行操作
        Ext.Viewport.unmask();
        Ext.Msg.alert('请求失败', '服务器维护中....');
    }

});